package com.wondertek.poms.core.utils;

/**
 * @author gaozhengxing gaozhenghang@wondertek.com.cn
 * @date 2021/1/3 14:25
 * @description TODO
 */
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

import com.wondertek.poms.common.utils.StringUtil;
import com.wondertek.poms.common.vo.ResultBean;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.i18n.LocaleContextHolder;

public class DateUtil {
    private static Log log = LogFactory.getLog(DateUtil.class);
    public static final String TIME_PATTERN = "HH:mm:ss";
    public static final String DATE_TIME_MS_PATTERN = "yyyy-MM-dd HH:mm:ss.S";
    public static final String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
    public static final String DATE_YYYYMMDD_PATTERN = "yyyyMMdd";
    public static final String DATE_YYYY_MM_DD_PATTERN = "yyyy-MM-dd";
    public static final String TIME_HHMM_PATTERN = "HH:mm";
    public static final String TIME_HHMM_PATTERN2 = "HHmm";
    public static final String DATE_TIME_NO_HORI_PATTERN = "yyyyMMdd HH:mm:ss";
    public static final String DATE_TIME_NO_SPACE_PATTERN = "yyyyMMddHHmmss";
    public static final String DATE_TIME_NO_SPACE_MS_PATTERN = "yyyyMMddHHmmssS";
    public static final String DATE_TIME_PLAYBILL_PATTERN = "yyyyMMdd HH:mm";
    public static final String DATE_TIME_INDEX_PLAYBILL_PATTERN = "yyyy-MM-dd HH:mm";
    public static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
    public static final String DATE_ENGLISH_FORMAT = "EEE MMM dd HH:mm:ss zzz yyyy";
    private static Map<String, SimpleDateFormat> patternFormatMap;
    private static final String BASIC_DATE = "2000-01-01 00:00:00";

    public static Map<String, SimpleDateFormat> getInstance() {
        if (patternFormatMap == null) {
            SimpleDateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            SimpleDateFormat yyyyMMdd = new SimpleDateFormat("yyyyMMdd");
            SimpleDateFormat yyyy_MM_dd = new SimpleDateFormat("yyyy-MM-dd");
            SimpleDateFormat HHmm = new SimpleDateFormat("HH:mm");
            SimpleDateFormat HHmm2 = new SimpleDateFormat("HHmm");
            SimpleDateFormat yyyyMMddHHmmss = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
            SimpleDateFormat yyyyMMddHHmmssFile = new SimpleDateFormat("yyyyMMddHHmmss");
            SimpleDateFormat yyyyMMddHHmmssSFile = new SimpleDateFormat("yyyyMMddHHmmssS");
            SimpleDateFormat PLAYBILL_TIME_PATTERN = new SimpleDateFormat("yyyyMMdd HH:mm");
            SimpleDateFormat INDEX_PLAYBILL_TIME_PATTERN = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            SimpleDateFormat ENGLISH_SDF = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.ENGLISH);
            patternFormatMap = new HashMap();
            patternFormatMap.put("yyyy-MM-dd HH:mm:ss.S", timeFormat);
            patternFormatMap.put("yyyy-MM-dd HH:mm:ss", dateFormat);
            patternFormatMap.put("yyyyMMdd", yyyyMMdd);
            patternFormatMap.put("HH:mm", HHmm);
            patternFormatMap.put("HHmm", HHmm2);
            patternFormatMap.put("yyyyMMdd HH:mm:ss", yyyyMMddHHmmss);
            patternFormatMap.put("yyyyMMddHHmmss", yyyyMMddHHmmssFile);
            patternFormatMap.put("yyyyMMdd HH:mm", PLAYBILL_TIME_PATTERN);
            patternFormatMap.put("EEE MMM dd HH:mm:ss zzz yyyy", ENGLISH_SDF);
            patternFormatMap.put("yyyy-MM-dd", yyyy_MM_dd);
            patternFormatMap.put("yyyy-MM-dd HH:mm", INDEX_PLAYBILL_TIME_PATTERN);
            patternFormatMap.put("yyyyMMddHHmmssS", yyyyMMddHHmmssSFile);
        }

        return patternFormatMap;
    }

    public static String formatDate(String pattern, Date adate) {
        if (adate == null) {
            return "";
        } else {
            SimpleDateFormat sdf = new SimpleDateFormat(pattern);
            return sdf.format(adate);
        }
    }

    public static Date parseDate(String pattern, String dateStr) {
        if (dateStr != null && !"".equals(dateStr)) {
            SimpleDateFormat sdf = new SimpleDateFormat(pattern);

            try {
                return sdf.parse(dateStr);
            } catch (ParseException var4) {
                var4.printStackTrace();
                return null;
            }
        } else {
            return null;
        }
    }

    private DateUtil() {
    }

    public static String strToString(String date) {
        if (date != null && !"".equals(date)) {
            String temp = "";

            try {
                Date dateStr = parseDate("yyyy-MM-dd HH:mm:ss", date);
                temp = formatDate("HH:mm", dateStr);
            } catch (Exception var3) {
                log.debug(var3.getStackTrace());
            }

            return temp;
        } else {
            return date;
        }
    }

    public static String strToStr(String date) {
        if (date != null && !"".equals(date)) {
            String temp = "";

            try {
                Date dateStr = parseDate("yyyy-MM-dd HH:mm:ss", date);
                temp = formatDate("yyyy-MM-dd HH:mm", dateStr);
            } catch (Exception var3) {
                log.debug(var3.getStackTrace());
            }

            return temp;
        } else {
            return date;
        }
    }

    public static String dateToString(Date date) {
        String returnValue = "";
        if (date != null) {
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            returnValue = df.format(date);
        }

        return returnValue;
    }

    public static String getDatePattern() {
        Locale locale = LocaleContextHolder.getLocale();

        String defaultDatePattern;
        try {
            defaultDatePattern = ResourceBundle.getBundle("ApplicationResources", locale).getString("date.format");
        } catch (MissingResourceException var3) {
            defaultDatePattern = "yyyy-MM-dd";
        }

        return defaultDatePattern;
    }

    public static String getDateTimePattern() {
        return getDatePattern() + " HH:mm:ss.S";
    }

    public static String getDate(Date aDate) {
        String returnValue = "";
        if (aDate != null) {
            SimpleDateFormat df = new SimpleDateFormat(getDatePattern());
            returnValue = df.format(aDate);
        }

        return returnValue;
    }

    public static Date convertStringToDate(String aMask, String strDate) throws ParseException {
        SimpleDateFormat df = new SimpleDateFormat(aMask);

        try {
            Date date = df.parse(strDate);
            return date;
        } catch (ParseException var5) {
            throw new ParseException(var5.getMessage(), var5.getErrorOffset());
        }
    }

    public static String getTimeNow(Date theTime) {
        return getDateTime("HH:mm:ss", theTime);
    }

    public static Calendar getToday() throws ParseException {
        Date today = new Date();
        SimpleDateFormat df = new SimpleDateFormat(getDatePattern());
        String todayAsString = df.format(today);
        Calendar cal = new GregorianCalendar();
        cal.setTime(convertStringToDate(todayAsString));
        return cal;
    }

    public static String getDateTime(String aMask, Date aDate) {
        SimpleDateFormat df = null;
        String returnValue = "";
        if (aDate == null) {
            log.error("aDate is null!");
        } else {
            df = new SimpleDateFormat(aMask);
            returnValue = df.format(aDate);
        }

        return returnValue;
    }

    public static String convertDateToString(Date aDate) {
        return getDateTime(getDatePattern(), aDate);
    }

    public static Date convertStringToDate(String strDate) {
        Date aDate = null;

        try {
            if (log.isDebugEnabled()) {
                log.debug("converting date with pattern: " + getDatePattern());
            }

            aDate = convertStringToDate(getDatePattern(), strDate);
        } catch (ParseException var3) {
            log.error("Could not convert '" + strDate + "' to a date, throwing exception");
            var3.printStackTrace();
        }

        return aDate;
    }

    public static java.sql.Date convertDateToSqlDate(Date date) {
        return new java.sql.Date(date.getTime());
    }

    public static Timestamp convertDateToTimestamp(Date date) {
        return new Timestamp(date.getTime());
    }

    public static String getNowTime(Date date) {
        return date == null ? "" : formatDate("yyyy-MM-dd HH:mm:ss.S", date);
    }

    public static String getDateTime(String sdate) {
        try {
            Timestamp date = stringToTimestamp(sdate);
            return formatDate("yyyy-MM-dd HH:mm:ss", date);
        } catch (Exception var2) {
            return sdate;
        }
    }

    public static Timestamp stringToTimestamp(String timestampStr) {
        return timestampStr != null && timestampStr.length() >= 1 ? Timestamp.valueOf(timestampStr) : null;
    }

    public static String[] getWholeWeekByDate(Date date) {
        String[] ss = new String[7];
        Calendar calendar = Calendar.getInstance();
        int i = 0;

        for (int j = 2; i < 6 && j < 8; ++j) {
            calendar.setTime(date);
            calendar.setFirstDayOfWeek(2);
            calendar.set(7, j);
            ss[i] = getFormatDate(calendar.getTime());
            ++i;
        }

        calendar.setTime(date);
        calendar.setFirstDayOfWeek(2);
        calendar.set(7, calendar.getFirstDayOfWeek() + 6);
        ss[6] = getFormatDate(calendar.getTime());
        return ss;
    }

    public static String getFormatDate(Date d) {
        return formatDate("yyyyMMdd", d);
    }

    public static String getHHmm2(Date d) {
        return formatDate("HHmm", d);
    }

    public static Date getDateByString(String pattern) throws ParseException {
        return parseDate("yyyyMMdd", pattern);
    }

    public static Date getPlayBillTimeByPattern(String date) throws ParseException {
        return parseDate("yyyyMMdd HH:mm", date);
    }

    public static String getNowTime() {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        String nowTime = df.format(date);
        return nowTime;
    }

    public static String getNowTimeNumber() {
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
        Date date = new Date();
        String nowTime = df.format(date);
        return nowTime;
    }

    public static Long getSeconds(Date date) {
        if (date == null) {
            return null;
        } else {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date basicDate = formatter.parse("2000-01-01 00:00:00", new ParsePosition(0));
            long secLong = (date.getTime() - basicDate.getTime()) / 1000L;
            return secLong;
        }
    }

    public static Long getSeconds(String dateStr, String df) {
        if (dateStr != null && !"".equals(dateStr)) {
            if (df == null || "".equals(df)) {
                df = "yyyy-MM-dd HH:mm:ss";
            }

            SimpleDateFormat formatter = new SimpleDateFormat(df);
            Date date = formatter.parse(dateStr, new ParsePosition(0));
            return getSeconds(date);
        } else {
            return null;
        }
    }

    public static Date getDateByStringyyyyMMddHHmmss(String pattern) throws ParseException {
        return parseDate("yyyyMMddHHmmss", pattern);
    }

    public static Date getFormatDateByEnglishSDF(String s) {
        return parseDate("EEE MMM dd HH:mm:ss zzz yyyy", s);
    }

    public static String getFormatDateByyyyyMMddHHmmssFile(Date d) {
        return formatDate("yyyyMMddHHmmss", d);
    }

    public static String formateStrDate(String d) {
        Date formateDate = parseDate("yyyy-MM-dd HH:mm:ss", d);
        String dateStr = getFormatDateByyyyyMMddHHmmssFile(formateDate);
        return dateStr;
    }

    public static String formatDate(String d) {
        Date formateDate = parseDate("yyyyMMddHHmmss", d);
        String dateStr = formatDate("yyyy-MM-dd", formateDate);
        return dateStr;
    }

    public static String formatLongToTimeStr(Long msl) {
        String str = "";
        Integer day = 0;
        Integer hour = 0;
        Integer minute = 0;
        Integer second = 0;
        Integer ms = 0;
        second = msl.intValue() / 1000;
        ms = msl.intValue() % 1000;
        if (second > 60) {
            minute = second / 60;
            second = second % 60;
        }

        if (minute > 60) {
            hour = minute / 60;
            minute = minute % 60;
        }

        if (hour > 24) {
            day = hour / 24;
            hour = hour % 24;
        }

        if (day > 0) {
            str = day.toString() + "天";
        }

        if (hour > 0) {
            str = str + hour.toString() + "小时";
        }

        if (minute > 0) {
            str = str + minute.toString() + "分钟";
        }

        if (second > 0) {
            str = str + second.toString() + "秒";
        }

        if (ms > 0) {
            str = str + ms.toString() + "毫秒";
        }

        return str;
    }

    public static String formatLongToTxt(Long msl) {
        String str = "";
        Integer day = 0;
        Integer hour = 0;
        Integer minute = 0;
        Integer second = 0;
        second = msl.intValue() / 1000;
        if (second > 60) {
            minute = second / 60;
            second = second % 60;
        }

        if (minute > 60) {
            hour = minute / 60;
            minute = minute % 60;
        }

        if (hour > 24) {
            day = hour / 24;
            hour = hour % 24;
        }

        if (day > 0) {
            str = day.toString() + "天";
        }

        if (hour > 0) {
            str = str + hour.toString() + "小时";
        }

        if (minute > 0) {
            str = str + minute.toString() + "分钟";
        }

        return str;
    }

    public static long getBetweenDays(Date t1, Date t2) throws ParseException {
        return (t2.getTime() - t1.getTime()) / 1000L / 60L / 60L / 24L;
    }

    public static int getIntervalDaysOfExitDate2(Date exitDateFrom, Date exitDateTo) {
        Calendar aCalendar = Calendar.getInstance();
        Calendar bCalendar = Calendar.getInstance();
        aCalendar.setTime(exitDateFrom);
        bCalendar.setTime(exitDateTo);
        int days = 0;

        while (aCalendar.before(bCalendar)) {
            ++days;
            aCalendar.add(6, 1);
        }

        return days;
    }

    public static Date parseDateNotLenient(String pattern, String dateStr) {
        if (dateStr != null && !"".equals(dateStr)) {
            SimpleDateFormat sdf = (SimpleDateFormat) getInstance().get(pattern);
            if (sdf == null) {
                sdf = new SimpleDateFormat(pattern);
                getInstance().put(pattern, sdf);
            }

            try {
                sdf.setLenient(false);
                return sdf.parse(dateStr);
            } catch (ParseException var4) {
                var4.printStackTrace();
                return null;
            }
        } else {
            return null;
        }
    }

    public static String getMarkByTime(long currentTimes, long times) {
        String result = "";
        long rate = 86400000L;
        long today = currentTimes / rate;
        long yesterday = today - 1L;
        long beforeYesterday = yesterday - 1L;
        long tomorrow = today + 1L;
        long input = times / rate;
        if (input == yesterday) {
            result = "yesterday";
        } else if (input == today) {
            result = "today";
        } else if (input == beforeYesterday) {
            result = "beforeYesterday";
        } else if (input == tomorrow) {
            result = "tomorrow";
        }

        return result;
    }

    public static String getNextMonth(String curMonth) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        String startDate = "";
        String endDate = "";
        String[] ta = curMonth.split("-");
        c.setTime(java.sql.Date.valueOf(curMonth + "-01"));
        c.add(2, 0);
        c.set(5, 1);
        sdf.format(c.getTime());
        c.set(2, Integer.parseInt(ta[1]));
        c.set(5, 1);
        c.add(6, 0);
        endDate = sdf.format(c.getTime());
        return endDate;
    }

    public static List<String> getMonthList(String startMonth, String endMonth) throws Exception {
        List<String> monthList = new ArrayList();
        monthList.add(startMonth);

        while (!startMonth.equals(endMonth)) {
            startMonth = getNextMonth(startMonth);
            if (startMonth != null && !startMonth.equals("")) {
                startMonth = startMonth.substring(0, 7);
                monthList.add(startMonth);
            }
        }

        return monthList;
    }

    public static String getFirstDayOfMonth(String yearMonth) {
        String[] tmp = yearMonth.split("-");
        int year = Integer.parseInt(tmp[0]);
        int month = Integer.parseInt(tmp[1]);
        Calendar cal = Calendar.getInstance();
        cal.set(1, year);
        cal.set(2, month - 1);
        cal.set(5, cal.getMinimum(5));
        return (new SimpleDateFormat("yyyy-MM-dd")).format(cal.getTime());
    }

    public static String getLastDayOfMonth(String yearMonth) {
        String[] tmp = yearMonth.split("-");
        int year = Integer.parseInt(tmp[0]);
        int month = Integer.parseInt(tmp[1]);
        Calendar cal = Calendar.getInstance();
        cal.set(1, year);
        cal.set(2, month - 1);
        cal.set(5, 1);
        int value = cal.getActualMaximum(5);
        cal.set(5, value);
        return (new SimpleDateFormat("yyyy-MM-dd")).format(cal.getTime());
    }

    public static String getYesterdayOfDay(String pattern, Date day) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(day);
        cal.add(6, -1);
        return (new SimpleDateFormat(pattern)).format(cal.getTime());
    }

    public static long time2MinusTime1(String time1, String time2) {
        Date createTimeBegin= StringUtil.nullToDate(time1,"yyyy-MM-dd");
        Date createTimeEnd= StringUtil.nullToDate(time2,"yyyy-MM-dd");
        return (createTimeEnd.getTime()-createTimeBegin.getTime())/(1000*60*60*24);
    }

    public static long time2MinusTime1(Date time1, Date time2) {
        return (time2.getTime()-time1.getTime())/(1000*60*60*24);
    }

}